package
{
  
  import com.longtailvideo.jwplayer.events.*;
  import com.longtailvideo.jwplayer.model.Model;
  import com.longtailvideo.jwplayer.model.PlayerConfig;
  import com.longtailvideo.jwplayer.player.*;
  import com.longtailvideo.jwplayer.plugins.*;
  import com.longtailvideo.jwplayer.utils.Configger;
  
  import flash.display.Loader;
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.events.IOErrorEvent;
  import flash.net.URLRequest;
  import flash.system.Security;
  import flash.utils.getDefinitionByName;
  
  public class VideoAS3JW extends Sprite
  {
    
      private var playerObject:Object;
      private var view:Object;
      
      private var playerLoader:Loader = new Loader();
      private var playerHolder:Sprite = new Sprite();
      
      public function VideoAS3JW():void {
        Security.allowDomain("*");
        loadPlayer();
        
        var p:Player = new Player();
      }
      
      public function loadPlayer():void {
        playerLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onPlayerLoaded);
//        playerLoader.load(new URLRequest("http://localhost/jwplayer.flash.swf?config=http://localhost/config.xml"));
        playerLoader.load(new URLRequest("http://localhost/player.swf?config=http://localhost/config.xml"));
//        playerLoader.load(new URLRequest("http://localhost/jwplayer.flash.swf?config=config.xml"));
      }
      
      private function onPlayerLoaded(event:Event) : void {
        playerObject = event.target.content as Object;
        var p:IPlayer = playerObject as IPlayer;
        var config:PlayerConfig = new PlayerConfig();
//        playerObject.config = config;
        playerObject.addEventListener(PlayerEvent.JWPLAYER_READY, onPlayerReady);
        playerObject.addEventListener(PlayerEvent.JWPLAYER_ERROR, onPlayerError);
        
        playerObject.config.file = "http://content.bitsontherun.com/videos/bkaovAYt-injeKYZS.mp4";
//        playerObject.config.image = "still.flv.jpg";
        playerObject.config.fullscreen = true;
        playerObject.config.controlbar = 'over';
        playerObject.config.autostart = true;
        
        addChild(playerLoader);
//        if(playerObject.view == null) {
//          playerObject.addEventListener(PlayerEvent.JWPLAYER_READY, onPlayerReady);
//        } else {
//          onPlayerReady();
//        }
      }
      
      private function onPlayerReady(event:Event = null):void {
        playerObject.config.resizing = false;
        playerObject.config.width = 640;
        playerObject.config.height = 360;
        
        //playerObject.view.sendEvent(ViewEvent.REDRAW);
        //playerObject.view.sendEvent(ViewEvent.PLAY);
      }
      
      private function onPlayerError(event:Event = null):void {
       trace("fd");
        
        //playerObject.view.sendEvent(ViewEvent.REDRAW);
        //playerObject.view.sendEvent(ViewEvent.PLAY);
      }
    
  }
}